<?php
class Controller_Admin_Budget extends  Controller_Template 
{
	public function __construct($req, $res) {
		$this->template = "smarty:admin/estimate/budgets";
		parent::__construct($req, $res);
	}

    public function action_index ()
    {
        $this->action_current();
    }

    /**
     * 当前待审核的预算信息
     */
    public function action_current ()
    {
	$role_id = user::current_user()->get_role_id();
	$user_id = user::current_user_id();
	$activities = Promotion_Activity::activity_by_manager($user_id);
	$ac_ids = array();
	if ($role_id == 'commonmanager' || $role_id == 'supermanager'){
	    $status = 'wait_business_confirm';
	}
	else{
	    foreach($activities as $val)
	    {
		$ac_ids[] = $val['id'];        
	    }

	    $status = 'wait_activity_confirm';

	}

	$query  = array(
		'status' => $status,
		'include_merchant_info' => TRUE,
		'include_activity_info' => TRUE,
		'include_channel_info'  => TRUE,
		'include_ratio'         => TRUE,
		);
	if (!empty($ac_ids))
	{
	    $query['activity'] = $ac_ids;
	}
    else if ($role_id == 'auditor')
    {
        $query['activity'] = array(-1);
    }
    else if ($role_id == 'supermanager')
    {
        unset($query['activity']);
    }


	$result = Estimate_Budget::search($query, $offset = NULL, $limit = NULL);
	$this->template->set_filename('smarty:admin/estimate/budget/current');
	$this->template->set('budgets', $result);
	$this->template->set('role_id', $role_id);
    }

    /**
     * 处理审核动作
     *
     */
    public function action_verify ($budget_id)
    {
        $post   = $this->request->post();
        $ratio  = Arr::get($post, 'ratio');
        $budget = new Estimate_Budget($budget_id);

        if (User::current_user()->get_role_id() == 'auditor')
            $status = $budget->confirm_by_project  ($ratio);
        else if (User::current_user()->get_role_id() == 'commonmanager' 
          || User::current_user()->get_role_id() == 'supermanager')
            $status = $budget->confirm_by_business ($ratio);
        else
            throw new Exception_Access("权限错误");

        $this->request->redirect('/budget');
    }
}
